package com.sxz.sbs.module.client;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.component.ClearEditText;
import com.sxz.sbs.applibrary.dialog.input.ValueInputBoardDialog;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.bean.CarListBean;
import com.sxz.sbs.module.client.presenter.AddCarPresenter;
import com.sxz.sbs.module.client.view.AddCarView;
import com.sxz.sbs.widget.CommonTitle;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/1/12
 * 添加车辆
 */
public class AddCarActivity extends BaseActivity<AddCarPresenter> implements AddCarView {

    @BindView(R.id.cet_add_carnum)
    ClearEditText cetAddCarNum;
    @BindView(R.id.cet_add_carprice)
    TextView cetAddCarPrice;
    @BindView(R.id.cet_add_carremark)
    ClearEditText cetAddCarRemark;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    private int customId;
    private String price = "";
    private CarListBean carListBean;

    @Override
    protected void init() {

        customId = getIntent().getIntExtra(IntentConstant.CUSTOM_ID, 0);
        carListBean = getIntent().getParcelableExtra("car_bean");
        if (carListBean != null) {
            new CommonTitle.Builder(this).setTitle(getString(R.string.title_change_car)).showMore(false).showRightText(true).build().show();
            price = carListBean.getTxt_CarPrice();
            cetAddCarNum.setText(carListBean.getTxt_CarNumber());
            cetAddCarPrice.setText(carListBean.getTxt_CarPrice() + "元");
            cetAddCarRemark.setText(carListBean.getTxt_Remark());
        } else {
            new CommonTitle.Builder(this).setTitle(getString(R.string.title_add_car)).showMore(false).showRightText(true).build().show();
        }

    }


    @OnClick({R.id.tv_title_right, R.id.cet_add_carprice})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cet_add_carprice:
                ValueInputBoardDialog dialog1 = new ValueInputBoardDialog(this, price, ValueInputBoardDialog.UnitEnum.SELL);
                dialog1.setOnDialogResultListener(new ValueInputBoardDialog.OnDialogResultListener() {
                    @Override
                    public void onResult(String value, String unit) {
                        price = value;
                        cetAddCarPrice.setText(value + unit);
                    }
                });
                dialog1.show();
                break;
            case R.id.tv_title_right:
                if (TextUtils.isEmpty(cetAddCarNum.getInputValue())) {
                    cetAddCarNum.setShakeAnimation();
                    return;
                }
                getAddCar();
                break;
            default:
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_car;
    }

    @Override
    protected AddCarPresenter createPresenter() {
        return new AddCarPresenter();
    }


    private void getAddCar() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        if (carListBean != null) {
            map.put("Custom_Id", carListBean.getCustom_Id());
            map.put("Id", carListBean.getId());
        } else {
            map.put("Custom_Id", customId);
        }
        map.put("txt_CarNumber", cetAddCarNum.getInputValue());
        map.put("txt_CarPrice", price);
        map.put("txt_Remark", cetAddCarRemark.getInputValue());
        mPresenter.getAddCar(map);
    }

    @Override
    public void onAddCarSuccess(String data) {
        finish();
    }
}
